package com.huawei.user.controller;


import com.huawei.user.dto.BinderDto;
import com.huawei.basic.exception.BusinessException;
import com.huawei.basic.util.AjaxResult;
import com.huawei.user.domain.LoginDto;
import com.huawei.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILogininfoService logininfoService;

    /**
     * 接口：用户登录
     */
    @RequestMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }

    }

    /**
     * 接口：微信登录
     */
    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 接口：微信绑定登录
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binderLogin(@RequestBody BinderDto binderDto){
        try {
            return logininfoService.binderLogin(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试~~");
        }
    }
    /**
     * 接口：微信登录
     */
    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable("token") String token){
        try {
            logininfoService.logout(token);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }
}
